<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liq <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_vote\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\qbt_vote\service\VoteService;
use plugins\qbt_vote\model\VoteItemModel;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    public function _initialize()
    {
        if('vote' != $this->request->param('_action')){
            $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
            if (!empty($adminId)) {
                $this->assign("admin_id", $adminId);
            } else {
                $this->error('未登录');
            }
        }
    }

    /**
     * 投票管理
     * @adminMenu(
     *     'name'   => '投票管理',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '千佰特投票管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $list =  Db::table('qbtcmf_vote')->select();
        $this->assign("list", $list);
        return $this->fetch('/admin_index');
    }

    //添加
    public function add(){
        if ($this->request->isAjax()){
            
            $data = $this->request->post();

            $service = new VoteService();
            $result = $service->add($data);
            if ($result){
                $this->success('添加成功', cmf_plugin_url('QbtVote://AdminIndex/index'));
            }else{
                $this->error('添加失败');
            }
        }else{
            return $this->fetch('/add');
        }
    }

    //编辑
    public function edit(){
        if ($this->request->isAjax()){
            $data = $this->request->post();
            
            $service = new VoteService();
            $result = $service->edit($data);
            if ($result){
                $this->success('保存成功', cmf_plugin_url('QbtVote://AdminIndex/index'));
            }else{
                $this->error('保存失败');
            }
        }else{
            $id = $this->request->param('id');
            $info =  Db::table('qbtcmf_vote')->where(['id'=>$id])->find();
            $start_time = date('Y-m-d H:i',$info['start_time']);
            $end_time = date('Y-m-d H:i',$info['end_time']);
            $info['city'] = $info['city']?explode(',',$info['city']):null;
            $this->assign('start_time',$start_time);
            $this->assign('end_time',$end_time);
            $this->assign("info", $info);
            return $this->fetch('/edit');
        }
    }


    //批量删除
    public function del(){
        $id = $this->request->param('ids/a');

        $id = implode(',',$id);
        if(empty($id)){
            $this->error('请选择对象');
        }
        $result = Db::name('vote')->where("id in ($id)")->delete();
        if ($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //删除
    public function del1(){
        $id = $this->request->param('id');
        $result = Db::name('vote')->where("id = $id")->delete();
        if ($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //禁用或启用
    public function ajaxstate(){
        $id = $this->request->param('ids/a');
        $id = implode(',',$id);
        if(empty($id)){
            $this->error('请选择对象');
        }
        $state = $this->request->param('state', 0, 'intval');
        $result = Db::table('qbtcmf_vote')->where("id in ($id)")->update(['state'=>$state]);
        if ($result){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
    
    
    //回答项
    public function item(){
        $vote_id = $this->request->param('vote_id');
        $result =  Db::name('vote_item')->where(['vote_id'=>$vote_id])->order('list_order asc,id asc')->select();
        $this->assign("list", $result);
        $this->assign('vote_id',$vote_id);
        return $this->fetch('/item');
    }
    
    //回答项添加
    public function item_add(){
        if($this->request->isAjax()){
            $data = $this->request->post();
            if(empty($data['name']) && empty($data['image'])){
                $this->error('文字和图片必须选一个');
            }
            $VoteItemModle = new VoteItemModel();
            $result = $VoteItemModle->allowField(true)->insert($data);
            if ($result){
                $this->success('添加成功', cmf_plugin_url('QbtVote://AdminIndex/item',['vote_id'=>$data['vote_id']]));
            }else{
                $this->error('添加失败');
            }
            
        }else{
            $Vote_id = $this->request->param('vote_id');
            $this->assign('Vote_id',$Vote_id);
            return $this->fetch('/item_add');
        }
        
    }
    
    
    //回答项编辑
    public function item_edit(){
        if($this->request->isAjax()){
            $data = $this->request->post();
            if(empty($data['name']) && empty($data['image'])){
                $this->error('文字和图片必须选一个');
            }
            $VoteItemModle = new VoteItemModel();
            $result = $VoteItemModle->allowField(true)->isUpdate(true)->save($data);
            if ($result!== false){
                $this->success('保存成功', cmf_plugin_url('QbtVote://AdminIndex/item',['vote_id'=>$data['vote_id']]));
            }else{
                $this->error('保存失败');
            }
    
        }else{
            $id = $this->request->param('id');
            $data = Db::name('vote_item')->where('id',$id)->find();
            $this->assign('data',$data);
            $this->assign('Vote_id',$data['vote_id']);
            return $this->fetch('/item_edit');
        }
    
    }
    
    public function item_ban()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $rst = Db::name('voteItem')->where(['id', $id])->update(['status' => 0]);
            if ($rst) {
                $this->success("隐藏成功！");
            } else {
                $this->error('隐藏失败！');
            }
            
        } else {
            $this->error('数据传入失败！');
        }
    }
    

    public function item_cancelBan()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $result = Db::name('voteItem')->where('id', $id)->update(['status' => 1]);
            if ($result) {
                $this->success("显示成功！");
            } else {
                $this->error('显示失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    public function listOrder()
    {
        $VoteItemModel = new  VoteItemModel();
        parent::listOrders($VoteItemModel);
        $this->success("排序更新成功！");
    }
    
    public function vote(){
        if($this->request->isAjax()){
            
            
            
            
            $id = $this->request->post('id/a');

            $service = new VoteService();
            $service->vote($id);
            
            
            
        }else{
            $id = $this->request->param('id', 0, 'intval');
            $service = new VoteService();
            $result = $service->info($id);
            $this->assign('vote',$result['vote']);
            $this->assign('item',$result['item']);
            $this->assign('maxcount',$result['maxcount']);
            return $this->fetch('/vote');
        }
        
    }
}
